define(function(require, exports, module) {
    var router = require("router");
    //var app = require("app");
    var controller = {};
    var goodslist={};
    var goods_spec={};
    controller.vue = function() {
        controller.vm = new Vue({
            router: router,
            data: function() {
                return {
                       shop_info:{},
                      shoppingCartList:[],
                      goods:{},
                      tabid:-1,
                      popup:0,
                      popup2:0,
                      data:{},
                      tableInfo:{},
                      choose_property_data: {},
                      choose_spec: {},
                      disabled: 0,
                      spec_keys_str: '',
                      goods_in_cart:{},
                      tag_list:[],
                      table_id:0,
                };
            },
            el: '#pageIndex',
            created: function() {
                this.fetchData();
            },
            watch: {
              shoppingCartList:function(val) {
                  // 购物车数量
                      var goods_in_cart ={};
                      var list = val.list;
                      for( var i in list){
                         list[i].idx=i;
                         goods_in_cart[list[i].goods_id]=list[i];
                      }
                      this.goods_in_cart={};
                      this.goods_in_cart=goods_in_cart;
              }
            },
            methods: {
                fetchData: function() {
                    var _this = this;
                    this.table_id=this.$route.params.table_id;
                    var user_info = app.getUserInfo();
                    if(this.$route.params.shop_id){
                      this.shop_id=this.$route.params.shop_id;
                      this.getTagList();       
                    }else{
                       this.getTableInfo();
                    }
                   
                    this.getcartByUserId();
                },
                 setData:function(arr){
                  for( var i in arr){
                     this[i] = arr[i];
                  }
                },
              

                btn_cancel:function(){
                  this.setData({ popup:0});
                },
                show_popup: function () {
                  if (this.shoppingCartList.count){
                    this.setData({ popup: 1 });
                  }
                },   
                  //桌位信息
              getTableInfo: function () {
                var that = this;
                app.get("mall/mall_tables/find", {
                  id: this.table_id
                }, function(res){
                  // 扫空码需要绑定商家信息
                  if(res.code==2){
                    window.location.href=app.params.apiDomain+"/apiv2/scene/bindTable?id="+that.table_id;
                    return ;
                   }

                  if (res.code != 200) {
                    app.alert(res.msg);
                  }
                  that.shop_id = res.data.shop_id;
                  that.setData({ tableInfo: res.data });
                 
                  that.getTagList();            
                });
              },
                // 标签列表
                getTagList:function(){
                  var that = this;
                  app.get("mall/tag/get", { shop_id: this.shop_id ,type:'2,12'}, function (res) {
                    if (res.code != 200) {
                      app.alert(res.msg);
                    }else if (res.data) {
                       that.tag_list = res.data.list;
                       that.showTabs(0);
                    }
                  });
                },
               
              //商品列表
                loadList:function(reload){
                  var that = this;
                  var attr_id = this.attr_id;
                  if( that.goods[attr_id] && that.goods[attr_id].length>0){
                     return ;
                  }
                  app.get("mall/goods/get", {
                      attr_id:this.attr_id,
                      status:1
                    },  (res)=> {
                      if (res.code != 200) {
                        app.alert(res.msg);
                      } 
                      
                      if (res.data && res.data.list ){
                       goodslist[attr_id] = res.data.list;
                      }else{
                       goodslist[attr_id] = [];
                      }
                      var that_goods=that.goods;
                      var goods=[];
                      for (var k in goodslist[attr_id]){
                        var item = goodslist[attr_id][k];
                        goods.push({
                          default_img: item['default_img'],
                          goods_name: item['goods_name'],
                          origin_price: item['origin_price'],
                          price: item['price'],
                          total_stock: item['total_stock'],
                          sub_title: item['sub_title'],
                          id:item['id'],
                        });
                      }
                      that.goods={};
                      that_goods[attr_id] = goods;
                      that.goods = that_goods;
                      // console.log(that.goods);
              
                    });
                },
                showTabs:function(index){
  
                  if (this.tabid == index) {
                    return;
                  }

                  var tag =  this.tag_list[index];
                  // console.log(tag);
                  this.attr_id = tag.id;
                  this.setData({ tabid: index });
                  this.loadList();
                   
                },
                // 购物车
                getcartByUserId:function(){
                  var that = this;
                  app.get("mall/cart/getcartByUserId", { 
                    table_id: this.table_id
                  }, function (res) {
                    if (res.code != 200) {
                      app.alert(res.msg);
                    } else if (res.data) {
                      that.setData({ shoppingCartList: res.data});
                      that.shop_info = res.data.shop_info;
                    }
                  });
                },
                //商品数量减
                minus: function (e) {
                  var target = $(e.target);
                  var id = target.attr("data-id");
                  var shoppingCartList = this.shoppingCartList;
                  if (shoppingCartList.list[id].count < 1) {
                    shoppingCartList.list[id].count = 0;
                  } else {
                    shoppingCartList.list[id].count--;
                  }
                  this.setData({ shoppingCartList: shoppingCartList });
                  this.addToCart({
                    count: shoppingCartList.list[id].count,
                    goods_spec_id:target.attr("data-goods_spec_id"),//  $(input).attr("goods_spec_id"),
                    goods_id: target.attr("data-goods_id"),
                  });

                },
                //商品数量加
                plus: function (e) {
                   var target = $(e.target);
                  var id = target.attr("data-id");
                 
                  var shoppingCartList = this.shoppingCartList;
                  shoppingCartList.list[id].count++;
                  this.setData({ shoppingCartList: shoppingCartList });
                  this.addToCart({
                    count: shoppingCartList.list[id].count,
                    goods_spec_id:target.attr("data-goods_spec_id"),//  $(input).attr("goods_spec_id"),
                    goods_id: target.attr("data-goods_id"),
                  });
                },
                //商品数量
                changeCount: function (e) {

                   var target = $(e.target);
                  var id = target.attr("data-id");
                  var count = target.val();
                  var shoppingCartList = this.shoppingCartList;
                  shoppingCartList.list[id].count = count;
                  this.setData({ shoppingCartList: shoppingCartList });
                  this.addToCart({
                    count: count,
                     goods_spec_id:target.attr("data-goods_spec_id"),//  $(input).attr("goods_spec_id"),
                    goods_id: target.attr("data-goods_id"),
                  });

                },
                //加入购物车
                 addToCart:function(post, callback){
 
                  var that = this;
                  post.table_id = this.table_id;
                  app.get("/mall/cart/add", post, function (res) {
                     
                    if (res.code != 200) {
                      app.alert(res.msg);
                    } else {
                      callback ? callback(res) : '';
                      that.setData({ shoppingCartList: res.data });
                      if (res.data.count === 0) {
                        that.btn_cancel();
                      }
                    }
                  });
                },
                //清空购物车
                clearCart:function(){
                 
                  var that = this;
                  app.get("mall/cart/clearCart", { shop_id: this.shop_id }, function (res) {
                    if (res.code != 200) {
                      app.alert(res.msg);
                    } else {
                      app.alert("恭喜,成功清空购物车!");
                       
                      that.setData({ shoppingCartList: { nextText:'去结算'}});
                      that.btn_cancel();
                    }
                    
                  });
                },
                //去结算
                settle_accounts:function(){
                    // var store_id = app.getStoreId();
                     this.$router.push({
                      name:'checkout',
                      params:{
                        table_id:this.table_id
                      }
                    });
                     // this.$router.push({name:'checkout',params:{shop_id:this.shop_id}});
                    // window.location.href = "/pages/shop/?store_id="+store_id+"#/checkout/"+this.shop_id;
                },
                //选规格

                buyNow: function (item_id,tab_id) {
                  // console.log(e);
                  var postData = {};
                  // var item_id = $(e.target).attr("data-item_id");
                  // var tab_id = $(e.target).attr("data-tab_id");
                  // console.log()
                  var data ;
                  if (tab_id>-1 && item_id>-1){
                    // console.log(goodslist);
                     data = goodslist[tab_id][item_id];
                    goods_spec = data.goods_spec; 
                    data.goods_spec={};
                  }else if (this.data.id){
                     data = this.data;
                  }else{
                    app.alert("请选择商品");
                    return ;
                  }
                 
                  if (data.property_arr.length > 0 && this.choose_spec.spec_stock > 0) {
                    postData.goods_id = this.choose_spec.goods_id;
                    postData.goods_spec_id = this.choose_spec.id;
                    //可以去结算
                  } else if (data.property_arr.length === 0) {
                    postData.goods_id = data.id;
                  } else if (this.popup2==1){
                     
                    app.alert('请选择商品规格');
                    return ; 
                  }else {
                    this.setData({data:data});
                    this.chooseSpec();
                    return;
                  }
                  //加入购车
                  this.addToCart(postData, ()=>{
                    // app.alert('成功加入购物车!');
                    
                  });
                  this.hide_choose_goods();
                },
                hide_choose_goods:function(){
                  goods_spec={};
                  this.setData({ popup2: 0, data: {}, choose_spec: {}, choose_property_data:{}});
                },
                //弹出选规格
                chooseSpec:function(){
                  this.setData({ popup2: 1 });
                },
                //选规格
                chooseProperty: function (e) {
                  //console.log(e); 
                  var property = e.currentTarget.dataset.property;
                  var subProperty = e.currentTarget.dataset.subProperty;
                  var choose_property_data = this.choose_property_data;
                  choose_property_data[property] = subProperty;
                  this.setData({ choose_property_data: choose_property_data });
                  var arr = [];
                  for (var i in choose_property_data) {
                    arr.push(choose_property_data[i]);
                  }

                  if (arr.length == this.data.property_arr.length) {
                    this.setData({ spec_keys_str: arr.join(" ") });
                    var l = arr.length;
                    for (var j = 0; j < (5 - l); j++) {
                      arr.push("");
                    }
                    // console.log(arr,arr);
                    var spec_keys = arr.sort();
                    var spec_keys_str = spec_keys.join("_");
                    if (goods_spec[spec_keys_str] && goods_spec[spec_keys_str].spec_stock > 0) {
                      this.setData({ choose_spec: goods_spec[spec_keys_str] });
                      this.setData({ disabled: 0 });
                    } else {
                      app.alert('已售空');
                       
                      this.setData({ disabled: 1 });
                    }
                  }

                  // console.log(this.choose_spec);
                  //choose_spec
                }
        }
            

        }).$mount('#app');
    };
    module.exports = controller;
});
